package com.erp.framework.model.vo;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;

import com.erp.framework.SimpleConstants;
import com.erp.framework.util.logger.Logger;

public class VersionableVo implements Versionable, Serializable, SimpleConstants{

    protected static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    private int updateVersion;

    private String createUser;
    private Timestamp createDate;
    private String updateUser;
    private Timestamp updateDate;
    private Timestamp lastModifiedTime;

    public VersionableVo() {
    }

    public String getCreateUser() {
        return createUser;
    }

    public Timestamp getCreateDate() {
        return createDate;
    }

    public String getUpdateUser() {
        return updateUser;
    }

    public Timestamp getUpdateDate() {
        return updateDate;
    }

    public int getUpdateVersion() {
        return updateVersion;
    }

    public Timestamp getLastModifiedTime() {
        return lastModifiedTime;
    }

    public void setCreateUser(String createUser) {
        this.createUser = createUser;
    }

    public void setCreateDate(Timestamp createDate) {
        this.createDate = createDate;
    }

    public void setUpdateUser(String updateUser) {
        this.updateUser = updateUser;
    }

    public void setUpdateDate(Timestamp updateDate) {
        this.updateDate = updateDate;
    }

    public void setUpdateVersion(int updateVersion) {
        this.updateVersion = updateVersion;
    }

    public void setLastModifiedTime(Timestamp lastModifiedTime) {
        this.lastModifiedTime = lastModifiedTime;
    }

    public boolean equalsUpdateVersion(int updateVersion) {
        return (this.updateVersion == updateVersion);
    }

    public int nextUpdateVersion() {
        updateVersion = (updateVersion < MAX_UPDATE_VERSION ? updateVersion + 1 : INITIAL_UPDATE_VERSION);
        return updateVersion;
    }

    public boolean isSameVersion(Versionable obj) {
        String verA = lastModifiedTime != null ? sdf.format(lastModifiedTime) : "";
        String verB = ((VersionableVo) obj).getUpdateDate() != null ? sdf.format(((VersionableVo) obj).getUpdateDate()) : "";
        Logger.trace(this, "verA= " + verA + ", verB = " + verB + ", verA same as verB? " + verA.equals(verB));
        return (verA.equals(verB));
    }
    
    public Collection getStrippableFields() {
        ArrayList list = new ArrayList();
        list.add("createUser");
        list.add("createDate");
        list.add("updateUser");
        list.add("updateDate");

        return list;
    }

    public boolean isCopyable() {
        return true;
    }
}
